package com.cndw.kungfu.event;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.jboss.netty.channel.group.ChannelGroupFuture;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import com.cndw.asyncsync.DataSyncServer;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsSystem;
import com.cndw.kungfu.config.SystemCache;
import com.cndw.kungfu.core.LogSyncServer;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.ActivitySerialize;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataSerialize;
import com.cndw.kungfu.model.PvpChallengeSerialize;
import com.cndw.kungfu.model.sys.SyncServer;
import com.cndw.rpg.framework.SessionHolder;

/**
 * 终止线程
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-5-27 下午09:51:41
 */
@Component
public class GameShutdownHandler extends Thread {

	public static final AtomicInteger debugOnDown = new AtomicInteger();

	private int state = 0;



	public void setState(int state) {
		this.state = state;
	}



	@Override
	public void run() {

		ConstantsSystem.setCloseState(2);

		long startTime = TimeProcesser.currentTimeMillis();
		try {
			ConstantsConfig.setSysClosed(true);
			SystemCache.getInstance().commitSync();
			DataSerialize.getInstance().commitSync();
			ActivitySerialize.getInstance().commitSync();
			PvpChallengeSerialize.getInstance().commitSync();
		} catch (Exception e) {
			e.printStackTrace();
		}

		SyncServer.setNeedClac(true);

		LogUtil.getMain().info("server shutdown begin...");
		int onlineNums = SessionHolder.getInstance().onlineNums();
		ChannelGroupFuture future = SessionHolder.getInstance().disconnectComm();

		LogUtil.getMain().info("onLine nums " + onlineNums);

		try {
			future.await(300, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		LogUtil.getMain().info("all channel disconnect");

		try {
			int sleepTime = onlineNums * 5;
			sleepTime = sleepTime <= 1000 ? 1000 : sleepTime;
			LogUtil.getMain().info("sleep time " + sleepTime);
			TimeUnit.MILLISECONDS.sleep(sleepTime);
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			LogUtil.getMain().info("TimeServer shutdown ...");
			Context.getTimerServer().shutdown();
		} catch (Exception e) {
			e.printStackTrace();
		}
		int dataSyncSize = DataSyncServer.getInstance().size();

		LogUtil.getMain().info("DataSyncServer size " + dataSyncSize);

		LogUtil.getMain().info("LogSyncServer size " + LogSyncServer.getInstance().size());
		int syncSize = dataSyncSize / 20;
		syncSize = NumberProcesser.between(syncSize, 20, 100);

		if (state == 0) {//
			DataSyncServer.getInstance().shutDown(0, syncSize);
		} else {
			LogUtil.getMain().info("LogSyncServer shutdown wait 600");
			DataSyncServer.getInstance().shutDown(600, syncSize);
		}

		LogSyncServer.getInstance().shutDown();

		LogUtil.getMain().info("server shutdown sync complete");

		LogUtil.getMain().info("sync user dataInfo " + debugOnDown.get());

		LogUtil.getMain().info("use time " + (TimeProcesser.currentTimeMillis() - startTime));

		LogUtil.getMain().info("server shutdown complete");

		try {
			String xx = new ClassPathResource("/").getFile().getParent();

			boolean isWindows = System.getProperty("os.name").startsWith("Windows");

			if (isWindows) {
				String command = "cmd /c start  " + xx + "\\web.bat";
				Runtime.getRuntime().exec(command);
			}

		} catch (IOException e2) {
			e2.printStackTrace();
		}

		ConstantsSystem.setCloseState(4);

	}

}
